<div ng-controller="picLibCtrl">
	<div class="modal-header">
		<button type="button" class="close" data-dismiss="modal">
			<span>&times;</span>
			<span class="sr-only">Close</span>
		</button>
		<h4 class="modal-title">图片库</h4>
	</div>
	<div class="modal-body" max-height="-95" lazy="search()">
		<div waterfall data="images" as="img">
			<span upload-image url="@{Resources.image_upload}" start="1" success="add" ext="png,jpg,jpeg,bmp"></span>
			<div class="fade-animate waterfall-elem" ng-click="select(img)" ng-class="selectedClass(img)" article="img" autoheight></div>
		</div>
	</div>
	<div class="modal-footer">
		<button type="button" class="btn btn-success" ng-click="${callback}(selected)" ng-disabled="btnDisabled()" data-dismiss="modal">选择图片</button>
		<button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
	</div>
</div>

<script type="text/javascript">
'use strict'
var picLibCtrl = ['$scope', '$http', function($scope, $http) {
	// 定义方法
	angular.extend($scope, {
		init: function() {
			$scope.selected = undefined;
		}, btnDisabled: function() {
			return angular.isUndefined($scope.selected);
		}, search: function() {
			// 初始化当前页号
			if (angular.isUndefined($scope.images_current)) $scope.images_current = 1;
			
			// 仅当总页数为空或小于等于当前页数时进行加载数据
			if (angular.isUndefined($scope.images_total) || $scope.images_total >= $scope.images_current) {
				$http({
					method: 'post',
					url: '@{Resources.image_search}',
					data: jQuery.param({page: $scope.images_current, size: 20}),
					headers: {'Content-Type': 'application/x-www-form-urlencoded'}
				}).success(function(data) {
					var parsed = [];
					angular.forEach(data.content, function(d, i) {
						parsed.push({id: d.id, picUrl: d.path});
					});
					$scope.images = parsed;
					$scope.images_total = data.totalPage;
					$scope.images_current = $scope.images_current + 1;
				});
			}
		}, selectedClass: function(img) {
			return (!angular.isUndefined(img) && $scope.selected === img) ? 'active' : '';
		}, select: function(img) {
			$scope.selected = ($scope.selected === img) ? undefined : img;
		}, add: function(img) {
			$scope['images_lane'][1].splice(0, 0, {id: img.id, picUrl: img.path});
		}
	});
	
	// 初始化
	$scope.init();
}];
</script>